import {
  snxd,
  img,
  cmslib
} from '../../config/env'
// import axios from 'axios'
import fetch from '@/config/fetch'
import {
  getPageParam,
  parseURL
} from '@/config/utils'

export const actions = {
  /**
   * 获取首页页面信息
   * @param {*} param0 
   */
  async getHomePage({
    commit,
    dispatch,
    state,
    rootState
  }) {
    console.log(state.position)
    let data = await fetch({
      url: snxd + '/home/getPoiInfoHomePageNew.do',
      params: {
        'storeLocation':rootState.position.storeLocation,
        'version':'3.2.2',
        'poiId': rootState.position.poiId,
        'cityCode': rootState.position.cityCode
      }
    }).then(res => {
      return res.data
    })
    commit('GET_HOME_PAGE', data)
    await dispatch('getHomeNav')
    dispatch('getHomeNotice')
    dispatch('getHomeContent')
  },
  /**
   * 获取导航信息
   * @param {*} param0 
   */
  async getHomeNav({
    commit,
    getters,
    dispatch
  }) {
    if (!getters.homePageUrl) {
      await dispatch('getHomePage')
    }
    let url_bf = getters.homePageUrl
    fetch({
      url: cmslib + '/api/jsonp/cb/' + url_bf + '-gethomenavjsonp.jsonp',
      params: {},
      jsonp: 'callback',
      jsonpCallback: 'gethomenavjsonp'
    }).then(res => {
      commit('GET_HOME_NAV', {
        homeNav: res.xd_noticebody.tag
      })
      commit('SET_INDEX_TAG',res.xd_noticebody.tag[0].elementDesc)
    })
  },

  /**
   * 获取公告信息
   * @param {*} param0 
   */
  async getHomeNotice({
    commit,
    getters,
    state
  }) {
    let url_gg = getters.homeNavUrl
    fetch({
      url: cmslib + '/api/jsonp/cb/' + url_gg + '-getbulletinurljsonp.jsonp',
      params: {},
      jsonp: 'callback',
      jsonpCallback: 'getbulletinurljsonp'
    }).then(res => {
      commit('GET_HOME_NOTICE', {
        homeNotice: res.xd_noticebody.tag
      })
    })
  },
  /**
   * 获取页面内容
   * @param {*} param0 
   */
  async getHomeContent({
    commit,
    getters,
    state
  }) {
    fetch({
      url: cmslib + '/api/aysn/page/' + getters.homeContentUrl + '.jsonp',
      params: {},
      jsonp: 'callback',
      jsonpCallback: 'cmsPageCallback'
    }).then(res => {
      commit('GET_HOME_CONTENT',res)
      commit('updateLoading',false)
    })
  }
}
